﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class addEditSemilate : Form
    {
        editEmployeeRecordsModule parent;
        TextBox txt;
        String val;
        int type;

        public addEditSemilate(editEmployeeRecordsModule parent, int type)
        {
            this.parent = parent;
            this.type = type;
            InitializeComponent();
            txt = (TextBox)semilateHours.Controls[1];
            txt.TextChanged += new EventHandler(txt_TextChanged);
            txt.Validating += new CancelEventHandler(txt_Validating);
            val = txt.Text;
            
            if (type == 1)
            {
                semilateDate.Value = DateTime.Parse(parent.getSelectedDate());
                semilateHours.Value = parent.getHours();
            }
        }

        private void addEditSemiLateButton_Click(object sender, EventArgs e)
        {
            if(semilateDate.Value>DateTime.Now)
                MessageBox.Show("Please set date at a previous date.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else if(semilateHours.Value>(parent.getEmployeeHours())-1)
                MessageBox.Show("Hours cannot be set to more than the hours the employee works.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
            {
                SemiLate semilate = new SemiLate();
                semilate.setDate(semilateDate.Value.ToString("yyyy-MM-dd"));
                semilate.setHours((int)semilateHours.Value);
                if(parent.getEmployeePayingBasis()=="Daily")
                    semilate.setCharge(parent.getEmployeeSalary()-((parent.getEmployeeSalary()/((double)parent.getEmployeeHours()))*((double)(semilateHours.Value)*5/6)));
                else
                    semilate.setCharge((parent.getEmployeeSalary()/28)-((parent.getEmployeeSalary()/28/((double)parent.getEmployeeHours()))*((double)(semilateHours.Value)*5/6)));

                if (type == 0)
                {
                    bool success = new SemilateController().addSemilate(semilate, parent.getSelectedLastName(), parent.getSelectedFirstName());
                    if (success)
                    {
                        parent.initializeContent();
                        parent.getEmployeesList().SelectedIndex = parent.getEmployeesList().Items.IndexOf(parent.getSelectedLastName() + ", " + parent.getSelectedFirstName());
                        this.Close();
                    }
                }
                else
                {
                    DateTime date = DateTime.Parse(parent.getSelectedDate());
                    bool success = new SemilateController().editSemilate(semilate, parent.getSelectedLastName(), parent.getSelectedFirstName(), date.ToString("yyyy-MM-dd"));
                    if (success)
                    {
                        parent.initializeContent();
                        parent.getEmployeesList().SelectedIndex = parent.getEmployeesList().Items.IndexOf(parent.getSelectedLastName() + ", " + parent.getSelectedFirstName());
                        this.Close();
                    }
                }
            }
        }

        private void addEditSemiLateCancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txt_TextChanged(object sender, EventArgs e)
        {
            if (txt.Focused)
                val = txt.Text;
        }

        private void txt_Validating(object sender, CancelEventArgs e)
        {
            if (multipleDecPoints(val))
            {
                MessageBox.Show("Invalid input; too many decimal points.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
                if (val.Equals("."))
                {
                    MessageBox.Show("Invalid input.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    double check = Double.Parse(val);
                    if (check < (double)semilateHours.Minimum || check > (double)semilateHours.Maximum)
                        MessageBox.Show("Invalid input; out of range.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
        }

        private bool multipleDecPoints(String num)
        {
            int nPoints = 0;
            int i = 0;
            while (nPoints < 2 && i < num.Length)
            {
                if (num[i] == '.')
                    nPoints++;
                i++;
            }
            if (nPoints < 2)
                return false;
            else
                return true;
        }
    }
}
